<!-- @module org-tree-select -->
<!-- @author: YunTao.Li -->
<!-- @description: 机构树形下拉框 -->
<!-- @since: 2019-12-01 01:58:10 -->
<template>
  <el-select ref="orgSelectRef" v-model="value" :value="value" :clearable="clearable" :disabled="disabled" @clear="clearHandle">
    <!-- <el-input
      v-model="filterText"
      class="selectInput"
      :placeholder="placeholder"
    /> -->

    <el-option :value="value" :label="valueTitle" :disabled="props.disabled" class="options">
      <el-tree
        id="tree-option"
        ref="selectTree"
        :accordion="accordion"
        :data="optionData"
        :props="props"
        :value="value"
        :node-key="props.value"
        :disabled="true"
        :default-expanded-keys="defaultExpandedKey"
        :filter-node-method="filterNode"
        @node-click="handleNodeClick"
      />
    </el-option>
  </el-select>
</template>

<script>
import { getPureOrganization } from '@/api/organization/org-service'

export default {
  name: 'ElTreeSelect',
  props: {
    /* 配置项 */
    props: {
      type: Object,
      default: () => {
        return {
          value: 'orgCode', // ID字段名
          label: 'orgName', // 显示名称
          disabled: false,
          children: 'children' // 子级字段名
        }
      }
    },
    /* 初始值 */
    value: {
      type: String,
      default: () => { return '' }
    },
    /* 可清空选项 */
    clearable: {
      type: Boolean,
      default: () => { return true }
    },
    /* 自动收起 */
    accordion: {
      type: Boolean,
      default: () => { return false }
    },
    placeholder: {
      type: String,
      default: () => { return '检索关键字' }
    },
    disabled: {
      type: Boolean,
      default: () => { return false }
    }
  },
  data() {
    return {
      filterText: '',
      valueId: '', // 初始值
      valueTitle: '',
      parentOrgId: '230',
      defaultExpandedKey: [],
      testList: [],
      cacheData: {},
      list: [
        { orgId: '', orgCode: '', parentOrgId: 0, orgName: '', orgLevel: 1, disabled: false }
      ]
    }
  },
  computed: {
    /* 转树形数据 */
    optionData() {
      const cloneData = JSON.parse(JSON.stringify(this.list)) // 对源数据深度克隆
      const formatterData = cloneData.filter(father => {
        // 循环所有项，并添加children属性
        const branchArr = cloneData.filter(child => {
          // if (child.orgCode === '01') {
          //   child.disabled = 'true'
          // }
          return father.orgId === child.parentOrgId
        }) // 返回每一项的子级数组
        branchArr.length > 0 ? (father.children = branchArr) : '' // 给父级添加一个children属性，并赋值
        return (father.parentOrgId === 0 || father.parentOrgId === this.parentOrgId) // 返回第一层
      })
      return formatterData
    }
  },
  watch: {
    value() {
      this.valueId = this.value
      this.initHandle()
    },
    valueId(val, oldVal) {
      if (val !== oldVal) {
        this.$emit('input', val)
      }
    },
    filterText(val) {
      this.$refs.selectTree.filter(val)
    }
  },
  mounted() {
    var _this = this || _this
    this.valueId = this.value
    this.initData().then(data => {
      _this.initHandle()
    })
  },
  methods: {
    // 初始化值
    initHandle() {
      if (this.valueId) {
        const nodeObject = this.$refs.selectTree.getNode(this.valueId)
        if (!nodeObject || nodeObject === null) {
          return
        }
        this.valueTitle = nodeObject.data[this.props.label] // 初始化显示
        this.$refs.selectTree.setCurrentKey(this.valueId) // 设置默认选中
        this.defaultExpandedKey = [this.valueId] // 设置默认展开
        // this.$emit('bindLabel', this.valueId, this.valueTitle)
      }
      this.initScroll()
    },
    initData() {
      return getPureOrganization(this.parentOrgId).then(response => {
        this.list = response.data
      })
    },
    // 初始化滚动条
    initScroll() {
      this.$nextTick(() => {
        const scrollWrap = document.querySelectorAll('.el-scrollbar .el-select-dropdown__wrap')[0]
        const scrollBar = document.querySelectorAll('.el-scrollbar .el-scrollbar__bar')
        scrollWrap.style.cssText = 'margin: 0px; max-height: none; overflow: hidden;'
        // eslint-disable-next-line no-return-assign
        scrollBar.forEach(ele => ele.style.width = 0)
      })
    },
    // 切换选项
    handleNodeClick(node) {
      this.valueTitle = node[this.props.label]
      this.valueId = node[this.props.value]
      this.defaultExpandedKey = []
      this.$refs.orgSelectRef.blur()
      var orgId = node['orgId']
      this.$emit('bindLabel', orgId, this.valueTitle)
    },
    // 清除选中
    clearHandle() {
      this.valueTitle = ''
      this.valueId = null
      this.defaultExpandedKey = []
      this.clearSelected()
      this.$emit('input', null)
      this.$emit('bindLabel', null)
    },
    /* 清空选中样式 */
    clearSelected() {
      const allNode = document.querySelectorAll('#tree-option .el-tree-node')
      allNode.forEach((element) => element.classList.remove('is-current'))
    },
    filterNode(value, data) {
      if (!value) return true
      return data.name.indexOf(value) !== -1
    }
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
  .el-scrollbar .el-scrollbar__view .el-select-dropdown__item{
    height: auto;
    max-height: 274px;
    padding: 0;
    overflow: hidden;
    overflow-y: auto;
  }
  .el-select{
    width: 100%
  }
  .el-select-dropdown__item.selected{
    font-weight: normal;
  }
  ul li >>>.el-tree .el-tree-node__content{
    height:auto;
    padding: 0 20px;
  }
  .el-tree-node__label{
    font-weight: normal;
  }
  .el-tree >>>.is-current .el-tree-node__label{
    color: #409EFF;
    font-weight: 700;
  }
  .el-tree >>>.is-current .el-tree-node__children .el-tree-node__label{
    color:#606266;
    font-weight: normal;
  }
  .selectInput{
    padding: 0 5px;
    box-sizing: border-box;
  }
  #app {
  font-family: "Avenir", Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}
</style>
